﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using agile_tool.userControl;

namespace agile_tool
{
    /// <summary>
    /// Interaction logic for sprintPage.xaml
    /// </summary>
    public partial class sprintPage : UserControl, IGenericPagesData
    {
        Button createNewSprint;
        create createsprintItem;
        int projectId;
        public string navigationName = "";
        int sprintDuration = 0;
        public sprintPage(int projectId, string filter,string projectName)
        {
            InitializeComponent();
            this.projectId = projectId;
            navigationName = projectName + " Sprint";
            refresh(filter);
        }
        public string getNavigationName()
        {
            return navigationName;
        }
        public int getId()
        {
            return projectId;
        }
        public void refresh(string filter)
        {
            lbSprints.Items.Clear();
            
            try
            {

                DataClasses1DataContext db = new DataClasses1DataContext();
                var sprints = from p in db.tblSprints where p.projectId == projectId select p;

                foreach (var p in sprints)
                {
                    if (filter == "")
                    {
                        sprintItem theSprintItem = new sprintItem();
                        theSprintItem.tbName.Text = p.name;
                        theSprintItem.tbDesc.Text = p.description;
                        try
                        {
                            sprintDuration = (int)p.tblProject.sprintDuration;
                        }
                        catch { sprintDuration = 0; }
                        if (p.startDate != null)
                        {
                            theSprintItem.dpStartDate.SelectedDate = p.startDate;
                            DateTime dt = (DateTime)p.startDate;
                            dt = dt.AddDays(sprintDuration);
                            theSprintItem.dpEndDate.SelectedDate = dt;
                        }
                        theSprintItem.id = p.id;
                        theSprintItem.btnDell.Click += new RoutedEventHandler(btnDell_Click);
                        theSprintItem.btnEdit.Click += new RoutedEventHandler(btnEdit_Click);
                        theSprintItem.btnAssign.Click += new RoutedEventHandler(btnAssign_Click);
                        theSprintItem.dpStartDate.SelectedDateChanged += new EventHandler<SelectionChangedEventArgs>(dpStartDate_SelectedDateChanged);
                        theSprintItem.btnBurnDown.Click += new RoutedEventHandler(btnBurnDown_Click);

                        lbSprints.Items.Add(theSprintItem);
                    }
                    else
                    {
                        if (p.name.IndexOf(filter) != -1)
                        {
                            sprintItem theSprintItem = new sprintItem();
                            theSprintItem.tbName.Text = p.name;
                            theSprintItem.tbDesc.Text = p.description;
                            try
                            {
                                sprintDuration = (int)p.tblProject.sprintDuration;
                            }
                            catch { sprintDuration = 0; }
                            if (p.startDate != null)
                            {
                                theSprintItem.dpStartDate.SelectedDate = p.startDate;
                                DateTime dt = (DateTime)p.startDate;
                                dt = dt.AddDays(sprintDuration);
                                theSprintItem.dpEndDate.SelectedDate = dt;
                            }
                            theSprintItem.id = p.id;

                            theSprintItem.btnAssign.Click += new RoutedEventHandler(btnAssign_Click);
                            theSprintItem.btnDell.Click += new RoutedEventHandler(btnDell_Click);
                            theSprintItem.btnEdit.Click += new RoutedEventHandler(btnEdit_Click);
                            theSprintItem.dpStartDate.SelectedDateChanged += new EventHandler<SelectionChangedEventArgs>(dpStartDate_SelectedDateChanged);
                            lbSprints.Items.Add(theSprintItem);

                        }
                    }

                }
            }
            catch { }
            createNewSprint = new Button { Content = "Create New Sprint" };
            createNewSprint.Click += new RoutedEventHandler(createNewSprint_Click);
            createNewSprint.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            lbSprints.Items.Add(createNewSprint);
        }
        Window w;
        void btnBurnDown_Click(object sender, RoutedEventArgs e)
        {
            lineChart lc = new lineChart();

            List<KeyValuePair<string, int>> valueList = new List<KeyValuePair<string, int>>();
            sprintItem theSprintItem = new sprintItem();
            theSprintItem = (sprintItem)helper.FindParrentUserControl((Button)sender, theSprintItem.GetType().ToString());


            DataClasses1DataContext db = new DataClasses1DataContext();
            var uss = from p in db.tblUserStorySprints where p.sprintId == theSprintItem.id select p;
            Dictionary<string, int> a = new Dictionary<string, int>();
            foreach (var f in uss)
            {
                var tasks = from p in db.tblTasks where p.userStoryId == f.userStoryId select p;
                foreach (var g in tasks)
                {

                    var taskHours = from p in db.tblTaskHours where p.taskId == g.id select p;
                    foreach (var h in taskHours)
                    {
                        lc.addValue((int)h.remaining, h.date.ToShortDateString());

                    }
                }
            }


            lc.createChart();
            w = new Window();
            w.Content = lc;
            w.Topmost = true;
            w.Show();
            lc.btnExit.Click += new RoutedEventHandler(btnExit_Click);
        }
       
        void btnExit_Click(object sender, RoutedEventArgs e)
        {
            w.Close();
            
        }

        void btnAssign_Click(object sender, RoutedEventArgs e)
        {
            sprintItem theSprintItem = new sprintItem();
            theSprintItem = (sprintItem)helper.FindParrentUserControl((Button)sender, theSprintItem.GetType().ToString());

            if (theSprintItem.tbName.Text == "")
            {
                MessageBox.Show("The Name Can't be Empty");
            }
            else if (theSprintItem.dpStartDate.SelectedDate == null)
            {
                MessageBox.Show("The Start Date Can't be Empty");
            }
            else
            {
                userStoryForSprintPage theUserStoryForSprintPage = new userStoryForSprintPage(theSprintItem.id, ((MainWindow)MainWindow.stateList[0]).tbSeatch.Text, theSprintItem.tbName.Text);
                MainWindow.stateList.Add(theUserStoryForSprintPage);
                ((MainWindow)MainWindow.stateList[0]).spBody.Children.Add(theUserStoryForSprintPage);
                ((MainWindow)MainWindow.stateList[0]).refreshNavButton();
                this.Visibility = Visibility.Collapsed;
            }
        }


        void dpStartDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            sprintItem theSprintItem = new sprintItem();
            theSprintItem = (sprintItem)helper.FindParrentUserControl((DependencyObject)sender, theSprintItem.GetType().ToString());
            DateTime dt = (DateTime)theSprintItem.dpStartDate.SelectedDate;
            dt = dt.AddDays(sprintDuration);
            theSprintItem.dpEndDate.SelectedDate = dt;


                if (theSprintItem.dpStartDate.SelectedDate == null)
                {
                    MessageBox.Show("The Start Date Can't be Empty");
                }
                else
                {
                    //((DateTime)theSprintItem.dpStartDate.SelectedDate)
                    theSprintItem.closeTheDesc();
                    theSprintItem.btnEdit.Content = "Edit";
                    DataClasses1DataContext db = new DataClasses1DataContext();
                    tblSprint tb = (from p in db.tblSprints where p.id == theSprintItem.id select p).First();
                    tb.name = theSprintItem.tbName.Text;
                    tb.description = theSprintItem.tbDesc.Text;
                    tb.startDate = ((DateTime)theSprintItem.dpStartDate.SelectedDate);
                    tb.endDate = ((DateTime)theSprintItem.dpEndDate.SelectedDate);

                    db.SubmitChanges();
                }
        }


        void btnTime_Click(object sender, RoutedEventArgs e)
        {
            
        }

    
       
        void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            sprintItem theSprintItem = new sprintItem();
            theSprintItem = (sprintItem)helper.FindParrentUserControl((Button)sender, theSprintItem.GetType().ToString());
            if (theSprintItem.spSlide.Height == 0)
            {
                theSprintItem.openTheDesc();
                theSprintItem.btnEdit.Content = "Save";
            }
            else
            {
                if (theSprintItem.tbName.Text == "")
                { 
                    MessageBox.Show("The Name Can't be Empty"); 
                }
                else if (theSprintItem.dpStartDate.SelectedDate == null)
                {
                    MessageBox.Show("The Start Date Can't be Empty");
                }
                //else if (theSprintItem.dpEndDate.SelectedDate == null)
                //{
                //    MessageBox.Show("The End Date Can't be Empty");
                //}
                //else if (((DateTime)theSprintItem.dpStartDate.SelectedDate) > ((DateTime)theSprintItem.dpEndDate.SelectedDate))
                //{
                //    MessageBox.Show("The Start Date Can't be After The End Date");
                //}
                //else if (((DateTime)theSprintItem.dpStartDate.SelectedDate) == ((DateTime)theSprintItem.dpEndDate.SelectedDate))
                //{
                //    MessageBox.Show("The Start Date Can't be Equal The End Date");
                //}
                else
                {
                    //((DateTime)theSprintItem.dpStartDate.SelectedDate)
                    theSprintItem.closeTheDesc();
                    theSprintItem.btnEdit.Content = "Edit";
                    DataClasses1DataContext db = new DataClasses1DataContext();
                    tblSprint tb = (from p in db.tblSprints where p.id == theSprintItem.id select p).First();
                    tb.name = theSprintItem.tbName.Text;
                    tb.description = theSprintItem.tbDesc.Text;
                    tb.startDate = ((DateTime)theSprintItem.dpStartDate.SelectedDate);
                    tb.endDate = ((DateTime)theSprintItem.dpEndDate.SelectedDate);

                    db.SubmitChanges();
                }
            }
        }


        internal static void deleteSprint(int id)
        {
            DataClasses1DataContext db = new DataClasses1DataContext();

            //var tsakHour = from p in db.tblTaskHours where p.tblTask.userStoryId == id select p;
            //db.tblTaskHours.DeleteAllOnSubmit(tsakHour);
            //db.SubmitChanges();
            
            //var tbac = from p in db.tblAcceptanceCriterias where p.userStoryId == id select p;
            //db.tblAcceptanceCriterias.DeleteAllOnSubmit(tbac);
            //db.SubmitChanges();

            //var tbtas = from p in db.tblTasks where p.userStoryId == id select p;
            //db.tblTasks.DeleteAllOnSubmit(tbtas);
            //db.SubmitChanges();

            var tbus = from p in db.tblUserStorySprints where p.id == id select p;
            foreach(var a in tbus){
            db.tblUserStorySprints.DeleteOnSubmit(a);
            db.SubmitChanges();
            }

            tblSprint tb = (from p in db.tblSprints where p.id == id select p).First();
            db.tblSprints.DeleteOnSubmit(tb);
            db.SubmitChanges();
        }

        void btnDell_Click(object sender, RoutedEventArgs e)
        {
            sprintItem theSprintItem = new sprintItem();
            theSprintItem = (sprintItem)helper.FindParrentUserControl(((Button)sender), theSprintItem.GetType().ToString());

            deleteSprint(theSprintItem.id);
            lbSprints.Items.Remove(theSprintItem);
        }

        void createNewSprint_Click(object sender, RoutedEventArgs e)
        {
            lbSprints.Items.Remove(createNewSprint);
            createsprintItem = new create();
            createsprintItem.btnCancel.Click += new RoutedEventHandler(btnCancel_Click);
            createsprintItem.btnSave.Click += new RoutedEventHandler(btnSave_Click);

            lbSprints.Items.Add(createsprintItem);
        }

        void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (createsprintItem.tbName.Text != "")
            {
                DataClasses1DataContext db = new DataClasses1DataContext();
                tblSprint tp = new tblSprint { name = createsprintItem.tbName.Text, description = createsprintItem.tbDesc.Text, projectId = projectId  };
                db.tblSprints.InsertOnSubmit(tp);
                db.SubmitChanges();

                refresh("");
            }
            else
            {
                MessageBox.Show("You need a name for your sprint");
            }
        }

        void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            refresh("");
        }

        
    }
}
